package com.etc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etc.entity.Patient;
import com.etc.service.PatientService;
import com.etc.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 就诊患者表(patient)表控制层
 *
 * @author xxxxx
 */
@Controller
@RequestMapping("/patient")
public class PatientController {
    /**
     * 服务对象
     */
    @Resource
    private PatientService patientService;

    /**
     * 查询全部
     */
    @GetMapping("select")
    public String selectAll(Model model, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                            @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Patient> patients = patientService.selectAll();
        PageInfo<Patient> pageInfo = new PageInfo<>(patients);
        model.addAttribute("patients", patients);
        model.addAttribute("pageInfo", pageInfo);
        return "select";
    }

    /**
     * 通过 Axios 查询全部
     */
    @GetMapping("selectByAxios")
    @ResponseBody
    public IPage<Patient> selectByAxios(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        Page<Patient> page = new Page<>(pageNum, pageSize);
        return patientService.page(page, null);
    }

    /**
     * 通过 Axios 模糊查询
     */
    @PostMapping("selectByLike")
    @ResponseBody
    public IPage<Patient> selectByLike(@RequestBody Patient patient, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        Page<Patient> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        if (patient != null) {
            if (patient.getName() != null)
                queryWrapper.like("name", patient.getName());
            if (patient.getCertificatesNo() != null)
                queryWrapper.like("certificates_no", patient.getCertificatesNo());
            if (patient.getPhone() != null)
                queryWrapper.like("phone", patient.getPhone());
        }
        return patientService.page(page, queryWrapper);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    @ResponseBody
    public Patient selectOne(Long id) {
        return patientService.selectByPrimaryKey(id);
    }

    /**
     * 跳转到新增页面
     *
     * @return String
     */
    @RequestMapping("/insertPage")
    public String insert() {
        return "insert";
    }

    /**
     * 跳转到修改页面
     *
     * @param model Model
     * @param id    主键
     * @return String
     */
    @RequestMapping("/updatePage/{id}")
    public String update(Model model, @PathVariable Long id) {
        Patient patient = patientService.selectByPrimaryKey(id);
        model.addAttribute("patient", patient);
        return "update";
    }

    /**
     * 新增患者
     *
     * @param patient Patient
     * @return int
     */
    @PostMapping("insert")
    public String insert(Patient patient) {
        patientService.insertSelective(patient);
        return "redirect:/patient/select";
    }

    @PostMapping("insertByAjax")
    @ResponseBody
    public String insertByAjax(Patient patient) {
        int result = patientService.insertSelective(patient);
        return result > 0 ? "success" : "error";
    }

    @PostMapping("insertByAxios")
    @ResponseBody
    public Result<Boolean> insertByAxios(@RequestBody Patient patient) {
        int result = patientService.insertSelective(patient);
        if (result > 0) {
            return Result.success(true);
        } else {
            return Result.error(false);
        }
    }

    /**
     * 修改患者信息
     *
     * @param patient Patient
     * @return int
     */
    @PostMapping("update")
    public String update(Patient patient) {
        patientService.updateByPrimaryKeySelective(patient);
        return "redirect:/patient/select";

    }

    /**
     * 通过 Axios 修改患者信息
     *
     * @param patient Patient
     * @return Result<Boolean>
     */
    @PutMapping("updateByAxios")
    @ResponseBody
    public Result<Boolean> updateByAxios(@RequestBody Patient patient) {
        int result = patientService.updateByPrimaryKeySelective(patient);
        if (result > 0) {
            return Result.success(true);
        } else {
            return Result.error(false);
        }
    }

    /**
     * 删除患者
     *
     * @param id Integer 主键
     * @return int
     */
    @GetMapping("delete/{id}")
    public String delete(@PathVariable Long id) {
        patientService.deleteByPrimaryKey(id);
        return "redirect:/patient/select";
    }

    @DeleteMapping("deleteByAxios")
    @ResponseBody
    public Result<Boolean> deleteByAxios(@RequestParam("id") Integer id) {
        int result = patientService.deleteByPrimaryKey(Long.valueOf(id));
        if (result > 0)
            return Result.success(true);
        else
            return Result.error(false);
    }
}
